/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-01 11:21:47
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2023-12-25 17:19:19
 * @FilePath: /yt-devops/lib/components/yt_expandable_tree/model/tree_mode.dart
 * @Description: 树节点model
 */

import 'package:yt_devops/utils/func_utils.dart';

///
Map<int, String> checkedMap = {
  0: '',
  1: 'partChecked',
  2: 'checked',
};

///

class TreeNode<T> {
  final String title;

  final dynamic value;

  dynamic prarentValue;

  /// 是否被选中
  int checked = 0;

  /// 是否被选中
  bool selected = false;

  /// 是否为叶子结点， 当设置为[true]时，则不再展示其子孙节点
  bool isLeaf;

  ///设置独立节点是否展示 Checkbox
  bool checkable;

  /// 是否可选中
  final bool selectable;

  /// 是否展开
  bool expanded;

  ///禁掉 checkbox 功能
  final bool disableCheckbox;

  /// 禁掉响应
  late final bool disabled;

  List<TreeNode<T>>? subNodes;

  TreeNode(this.value,
      {this.title = '',
      this.isLeaf = false,
      this.subNodes,
      this.checkable = true,
      this.selectable = false,
      this.expanded = false,
      this.disableCheckbox = false,
      this.disabled = false});

  bool get isLastLeaf => isLeaf == true || FuncUtils.isListEmpty(subNodes);

  static List<TreeNode<T>> listFrom<T>({
    required List<T> source,
    required TreeNodeProp<T, dynamic> value,
    required TreeNodeProp<T, String> title,
    TreeNodeProp<T, List<TreeNode<T>>>? subNodes,
    TreeNodeProp<T, bool>? checkable,
    TreeNodeProp<T, bool>? selectable,
    TreeNodeProp<T, bool>? disableCheckbox,
    TreeNodeProp<T, bool>? disabled,
    TreeNodeProp<T, bool>? isleaf,
  }) =>
      source
          .asMap()
          .map((index, item) => MapEntry(
              index,
              TreeNode<T>(
                value.call(index, item),
                title: title.call(index, item),
                subNodes: subNodes?.call(index, item),
                checkable: checkable?.call(index, item) ?? true,
                selectable: selectable?.call(index, item) ?? true,
                disableCheckbox: disableCheckbox?.call(index, item) ?? false,
                disabled: disabled?.call(index, item) ?? false,
                isLeaf: isleaf?.call(index, item) ?? false,
              )))
          .values
          .toList();
}

typedef TreeNodeProp<K, V> = V Function(int index, K item);
